package com.rpj.stauy.config;

import com.rpj.stauy.service.MemoryChatAssistant;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.StreamingChatModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.openai.OpenAiStreamingChatModel;
import dev.langchain4j.service.AiServices;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MenoryConfig {

    @Bean
    public ChatModel chatModel(){
        return OpenAiChatModel.builder()
                .apiKey(System.getenv("aliQwen_api"))
                .modelName("qwen-plus")
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                .build();
    }
    @Bean
    public StreamingChatModel streamChatModel(){
        return OpenAiStreamingChatModel.builder()
                .apiKey(System.getenv("aliQwen_api"))
                .modelName("qwen-plus")
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                .logRequests(true)
                .logResponses(true)
                .build();
    }

    @Bean("memory")
    public MemoryChatAssistant memoryChatAssistant(ChatModel chatModel ){
        //构建者模式，使高级api具备多个参数构建
        return AiServices.builder(MemoryChatAssistant.class)
                .chatModel(chatModel)
                .chatMemoryProvider(memoryid-> MessageWindowChatMemory.withMaxMessages(10))
                .build();
    }

    @Bean("memoryAndStream")
    public MemoryChatAssistant memoryAndStreamChatAssistant(StreamingChatModel streamingChatModel ){
        //构建者模式，使高级api具备多个参数构建
        return AiServices.builder(MemoryChatAssistant.class)
                .streamingChatModel(streamingChatModel)
                .chatMemoryProvider(memoryid-> MessageWindowChatMemory.withMaxMessages(10))
                .build();
    }
}
